package cn.neusoft.service.impl;

import cn.neusoft.bean.Deliveryaddress;
import cn.neusoft.dao.IDeliveryaddressDao;
import cn.neusoft.dao.impl.DeliveryaddressDao;
import cn.neusoft.query.BaseQuery;
import cn.neusoft.query.DeliveryaddressQuery;
import cn.neusoft.service.IDeliveryaddressService;
import cn.neusoft.vo.PageBean;
import cn.neusoft.vo.ResultBean;
import com.alibaba.druid.util.StringUtils;

import java.util.List;

public class DeliveryaddressService implements IDeliveryaddressService {
    private IDeliveryaddressDao deliveryaddressDao;

    public DeliveryaddressService() {
        deliveryaddressDao = new DeliveryaddressDao();
    }

    @Override
    public ResultBean add(Deliveryaddress deliveryaddress) throws Exception {
        if (deliveryaddress==null || StringUtils.isEmpty(deliveryaddress.getContactName())
                ||StringUtils.isEmpty(deliveryaddress.getAddress())
                ||StringUtils.isEmpty(deliveryaddress.getContactTel())){
            return ResultBean.ERROR("params null");
        }
        int i = deliveryaddressDao.add(deliveryaddress);
        return i>0?ResultBean.SUCCESS():ResultBean.ERROR("add deliveryaddress is fail");
    }

    @Override
    public ResultBean update(Deliveryaddress deliveryaddress) throws Exception {
        if (deliveryaddress==null ||StringUtils.isEmpty(deliveryaddress.getContactName())
                ||StringUtils.isEmpty(deliveryaddress.getAddress())
                ||StringUtils.isEmpty(deliveryaddress.getContactTel())){
            return ResultBean.ERROR("params null");
        }
        int i = deliveryaddressDao.update(deliveryaddress);
        return i>0?ResultBean.SUCCESS():ResultBean.ERROR("update deliveryaddress is fail");
    }

    @Override
    public ResultBean delete(Integer id) throws Exception {
        if (id==null){
            return ResultBean.ERROR("params null");
        }
        int i = deliveryaddressDao.delete(id);
        return i>0?ResultBean.SUCCESS():ResultBean.ERROR("delete deliveryaddress is fail");
    }

    @Override
    public ResultBean getById(Integer id) throws Exception {
        if (id==null){
            return ResultBean.ERROR("params null");
        }
        Deliveryaddress deliveryaddress = deliveryaddressDao.getById(id);
        return deliveryaddress==null?ResultBean.ERROR("get food by id is fail"):ResultBean.SUCCESS(deliveryaddress);

    }

    @Override
    public ResultBean getAll() throws Exception {
        List<Deliveryaddress> deliveryaddresses = deliveryaddressDao.getAll();
        return ResultBean.SUCCESS(deliveryaddresses);
    }

    @Override
    public ResultBean getByPage(BaseQuery baseQuery) throws Exception {

        if (baseQuery==null){
            baseQuery = new DeliveryaddressQuery();
        }

        PageBean<Deliveryaddress> pageBean = new PageBean<>();

        int count = deliveryaddressDao.getCount(baseQuery);
        pageBean.setTotalRows(count);

        if(count%baseQuery.getPageCount()==0){
            pageBean.setTotalPages(count/baseQuery.getPageCount());
        }else{
            pageBean.setTotalPages(count/baseQuery.getPageCount()+1);
        }

        List<Deliveryaddress> deliveryaddressList = deliveryaddressDao.getByPage(baseQuery);
        pageBean.setData(deliveryaddressList);

        return ResultBean.SUCCESS(pageBean);
    }

    @Override
    public ResultBean bathDel(List<Integer> ids) throws Exception {
        if (ids == null || ids.size()==0){
            return ResultBean.ERROR("params null");
        }

        for (Integer id:ids){
            deliveryaddressDao.delete(id);
        }
        return ResultBean.SUCCESS();

    }
}
